package Thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Dom45 {
    public static void main(String[] args) throws InterruptedException {
        //现在把整个任务拆成10个部分，每个部分视为一个”子任务“
        //可以把这10个子任务丢到线程池当中，让线程池执行
        //当然也可以安排10个独立的线程执行
        CountDownLatch latch=new CountDownLatch(10);
        ExecutorService executor= Executors.newFixedThreadPool(4);
        for (int i = 0; i < 10; i++) {
            int id=i;
            executor.submit(()->{
                System.out.println("子任务开始执行：" + id);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子任务结束执行：" + id);
                latch.countDown();
            });
        }
        //这个方法阻塞等待所有的任务结束
        latch.await();
        System.out.println("所有任务执行完毕");
        executor.shutdown();
    }
}
